<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/8
 * Time: 下午4:36
 */

namespace Api\Service;


use Mobile\Model\memberModel;
use Mobile\Model\moneylogModel;
use Think\Exception;

class UserService extends BaseService
{
    protected $userModel;
    protected $memberModel;
    protected $moneylogModel;
    public function __construct()
    {
        $this->userModel=M('member');
        $this->moneylogModel=new moneylogModel();
        $this->memberModel=new memberModel();
    }

    /**
     * @param $where
     * @param $filed
     * @return bool|mixed
     */
    public function findUserInfo($where,$filed=''){
        try{
            if(empty($where)){
                return false;
            }
            if(empty($filed)){
                return $this->userModel->where($where)->find();
            }else{
                return $this->userModel->field($filed)->where($where)->find();
            }
        }catch (Exception $ex){
            return false;
        }

    }

    /**
     * 新增
     * @param $add
     * @return mixed
     */
    public function add($add){
        return $this->userModel->add($add);
    }

    /**
     * 查询用户信息
     * @param $where
     * @param $page
     * @param $pageNum
     * @param $filed
     * @return mixed
     */
    public function queryUserInfo($where,$page=1,$pageNum,$filed){
        if(!empty($filed)){
            return $this->userModel->field($filed)->where($where)->page($page,$pageNum)->select();
        }else{
            return $this->userModel->where($where)->page($page,$pageNum)->select();
        }
    }

    //查询总条数
    public function countUser($where){
        return $this->userModel->where($where)->count();
    }

    /**
     * 查询用户信息以及等级
     * @param $where
     * @param $page
     * @param $pageNum
     * @param $filed
     * @return mixed
     */
    public function queryUserInfoOfGrade($where,$page=1,$pageNum,$filed=null){
        $re=$this->userModel
            ->field($filed)
            ->join('waoo_grade on waoo_member.grade_id=waoo_grade.grade_id')
            ->where($where)
            ->page($page,$pageNum)
            ->select();
       //echo  $this->userModel->getLastSql();die;
        return $re;
    }

    /*
   * 扣除微币,并发送微信模板消息通知
   */
    public function _kou_score($order,$memberinfo){

        //$mtype 1分销佣金,2重消币 3现金电子币  4重消佣金 5互助奖6车房奖7领导奖8分红奖 9保证金 10个税
        //现金币
        $this->_outMoneylog($order['cash_money'],$order,3);

        //佣金TODO:区分重消佣金,分销佣金
        $this->_outMoneylog($order['yongjin_money'],$order,1);

        $this->_outMoneylog($order['prize_money'],$order,12);

        $this->_outMoneylog($order['ex_cash'],$order,4);

        $this->_outMoneylog($order['score_money'],$order,2);

        return true;
    }
    /*
    * 写支出流水,并扣除用户表里的钱
    * $mtype 1分销佣金,2重消币 ,3现金电子币 ,4重消佣金,5互助奖,6车房奖, 7领导奖, 8分红奖 ,9保证金 , 10个税 , 11提货币流水
    */
    public function _outMoneylog($money,$order,$mtype){


        if($money == 0){
            return false;//金额等于0 不做任何操作
        }

        $uid = $order['uid'];

        switch($mtype){
            case 2:
                $this->memberModel->kouQian($uid,'score',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['score'];
                MoneyLogService::S()->_money_msg($userinfo, $money, 2);
                break;
            case 3:
                $this->memberModel->kouQian($uid,'cash_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['cash_money'];
                MoneyLogService::S()->_money_msg($userinfo, $money, 3);
                break;
            case 1:
                $this->memberModel->kouQian($uid,'money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['money']+$userinfo['lock_money'];
                MoneyLogService::S()->_money_msg($userinfo, $money, 1);
                break;
            case 12:
                $this->memberModel->kouQian($uid,'prize_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['prize_money']+$userinfo['lockprize_money'];
                break;
            case 4:
                $this->memberModel->kouQian($uid,'stock',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['stock'];
                MoneyLogService::S()->_money_msg($userinfo, $money, 4);
                break;
            default:
                return false;
        }

        $money_log = array(
            "uid" => $order['uid'],
            "nickname" => $order['realname'],
            "logtype" => 0,//收入 1收入 0支出
            "mtype" => $mtype,//类型
            "money" => $money,
            "content" => "【订单消费】".$order['order_title'],
            "order_id" => $order['order_id'],
            "order_uid" => $order['uid'],
            "order_paymoney" => $order['order_cashmoney'],//订单支付的金额(需要分佣的金额)
            "order_nickname" => $order['nickname'],
            "create_time" => date("Y-m-d H:i:s"),//写流水的时间
            "un_lock" => 1,//不锁定,立即扣除 1解锁,0锁定
            "cash_money" => $yue,//查余额
            "grade_id" => $userinfo['grade_id']
        );


        $res = $this->moneylogModel->addMoneylog($money_log);

        return $res;

    }

    public function _inMoneylog($money,$order,$mtype){

        if($money == 0){
            return false;//金额等于0 不做任何操作
        }

        $uid = $order['uid'];

        switch($mtype){
            case 2:
                $this->memberModel->jiaQian($uid,'score',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['score'];
                break;
            case 3:
                $this->memberModel->jiaQian($uid,'cash_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['cash_money'];
                break;
            case 1:
                $this->memberModel->jiaQian($uid,'money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['money']+$userinfo['lock_money'];
                break;
            case 12:
                $this->memberModel->jiaQian($uid,'prize_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['prize_money']+$userinfo['lockprize_money'];
                break;
            case 4:
                $this->memberModel->jiaQian($uid,'stock',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->find();
                $yue = $userinfo['stock'];
                break;
            default:
                return false;
        }

        $money_log = array(
            "uid" => $order['uid'],
            "nickname" => $order['realname'],
            "logtype" => 1,//收入 1收入 0支出
            "mtype" => $mtype,//类型
            "money" => $money,
            "content" => "补货订单".$order['order_title'],
            "order_id" => $order['order_id'],
            "order_uid" => $order['uid'],
            "order_paymoney" => $order['order_cashmoney'],//订单支付的金额(需要分佣的金额)
            "order_nickname" => $order['nickname'],
            "create_time" => date("Y-m-d H:i:s"),//写流水的时间
            "un_lock" => 1,//不锁定,立即扣除 1解锁,0锁定
            "cash_money" => $yue,//查余额
        );

        $res = $this->moneylogModel->addMoneylog($money_log);

        return $res;

    }


    //查询单个字段
    public function findFiled($uid,$filed){
       return  $this->userModel->where(array('uid'=>$uid))->getField($filed);
    }

    //更新用户信息
    public function updateMemberInfoByUid($uid,$data){
        $where = array(
            'uid' => $uid
        );
        if(empty($data) || empty($uid)){
            return false;
        }
        $res = $this->memberModel->editMember($where, $data);
        return $res;
    }



}